/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */
import {mapActions, mapGetters} from 'vuex';
import { on as bindEvent, off as unbindEvent } from 'wind-dom';
import selectDataTransfrom from 'src/util/select_transform.ts';

export default {
    data () {
        return {
            editFlowName:''
        };
    },
    computed: {
        ...mapGetters([
            'aiFlowInfo'
        ]),
        projectId () {
            return this.$route.query.projectId || '';
        }
    },

    watch: {
        aiFlowInfo:{
            deep:true,
            immediate:true,
            handler () {
                if (!this.isNullObject(this.aiFlowInfo)) {
                    bindEvent(window, 'beforeunload', this._onBeforeunload);
                    this.$once('hook:beforeDestroy', () => {
                        unbindEvent(window, 'beforeunload', this._onBeforeunload);
                    });
                }
            }
        }
    },
    methods: {
        ...mapActions([
            'saveAiFlowInfo'
        ]),
        _onBeforeunload (evt) {
            evt = evt || window.event;

            // 兼容IE8和Firefox 4之前的版本
            let msg = _('AI Flow配置尚未保存，是否离开当前页面？');
            if (evt) {
                evt.returnValue = msg;
            }

            // Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
            return msg;
        },
        isNullObject (data) {
           return  JSON.stringify(data) === '{}';
        },
        getTransformFieldList (obj) {
            let transformList = Object.keys(obj);
            let fieldList = [];
            fieldList = selectDataTransfrom(transformList);
            return fieldList;
        },
        _addListId (list) {
            list.forEach((item, index)=>{
                list[index].id = index + 1;
            });
            return list;
        }
        
    }
};
